package cn.mywcc.filter;


import cn.mywcc.feign.UserTokenChecker;
import cn.mywcc.util.Rs4Filter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Map;

@Component
public class LoginCheckerFilter implements GlobalFilter, Ordered {
    @Autowired
    UserTokenChecker userTokenChecker;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        if (shouldSkip(exchange))return chain.filter(exchange);
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String token = request.getHeaders().getFirst("token");
        if (StringUtils.hasText(token)){
            Map<String, String> checkRs = userTokenChecker.check(token);
            if (!checkRs.containsKey("check")||!checkRs.get("check").equals("ok"))return response.writeWith(Rs4Filter.createData(response,HttpStatus.EXPECTATION_FAILED,"请重新登录,check loss"));
            if (!checkRs.containsKey("uid")||!StringUtils.hasText(checkRs.get("uid")))return response.writeWith(Rs4Filter.createData(response,HttpStatus.EXPECTATION_FAILED,"请重新登录,uid loss"));
            if (!checkRs.containsKey("roleIds")||!StringUtils.hasText(checkRs.get("roleIds")))return response.writeWith(Rs4Filter.createData(response,HttpStatus.EXPECTATION_FAILED,"请重新登录,roleIds loss"));
            if (!checkRs.containsKey("groupId")||!StringUtils.hasText(checkRs.get("groupId")))return response.writeWith(Rs4Filter.createData(response,HttpStatus.EXPECTATION_FAILED,"请重新登录,groupId loss"));
            /*检测token是OK的*/
            ServerHttpRequest mutatedRequest = request.mutate()
                    .header("uid", checkRs.get("uid"))
                    .header("roleIds", checkRs.get("roleIds"))
                    .header("groupId", checkRs.get("groupId"))
                    .build();
            return chain.filter(exchange.mutate().request(mutatedRequest).build());
        }
        return response.writeWith(Rs4Filter.createData(response,HttpStatus.UNAUTHORIZED,"请先登录,token loss"));
    }

    private boolean shouldSkip(ServerWebExchange exchange) {
        // 判断是否应该跳过认证
        String path = exchange.getRequest().getURI().getPath();
        System.out.println("path:"+path);
        return path.matches("^/api/public/.*")
                ||path.matches("^/api/image/.*")
                ; // 例如，所有以/api/public/开头的路径
    }
    @Override
    public int getOrder() {
        return 1;
    }
}
